from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import (
    QFrame,
    QLabel,
    QMessageBox,
    QScrollArea,
    QVBoxLayout,
    QWidget,
)


class ScrollMessageBox(QMessageBox):
    """
    https://stackoverflow.com/questions/47345776/pyqt5-how-to-add-a-scrollbar-to-a-qmessagebox
    show multi line of message str with scroll bar
    """

    def __init__(self, msgList, width=450, *args, **kwargs):
        """
        init message box content
        Parameters
        ----------
        msgList : |a iterable of str|
            message box content item
        *args :
            Others parameters are passed to the QMessageBox |__init__| function.
        **kwargs :
            Others parameters are passed to the QMessageBox |__init__| function.
        """
        QMessageBox.__init__(self, *args, **kwargs)
        scroll = QScrollArea(self)
        scroll.setFrameShape(QFrame.NoFrame)
        scroll.setFrameShadow(QFrame.Plain)
        scroll.setWidgetResizable(True)
        scroll.setAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
        scrollMinHeight = 0
        self.content = QWidget()
        scroll.setWidget(self.content)
        layout = QVBoxLayout(self.content)
        for msg in msgList:
            label = QLabel(msg, self)
            layout.addWidget(label)
            scrollMinHeight += label.height()
        self.layout().addWidget(scroll, 0, 0, 1, self.layout().columnCount())
        if scrollMinHeight <= 60:
            scrollMinHeight = 60
        elif scrollMinHeight > 400:
            scrollMinHeight = 400
        self.setStyleSheet("QScrollArea{{min-width:{} px; min-height: {}px}}".format(width, scrollMinHeight))
        # self.setFocus()
        # import pdb; pdb.set_trace()
